home *** CD-ROM | disk | FTP | other *** search
/ Programming an RTS Game with Direct3D / Programming an RTS Game with Direct3D.iso / Examples / Chapter 13 / Example 13.1 / app.cpp next >
Encoding:
C/C++ Source or Header  |  2006-09-21  |  6.5 KB  |  261 lines

  1. //////////////////////////////////////////////////////////////
  2. // Example 13.1: Playing Sounds                                //
  3. // Written by: C. Granberg, 2006                            //
  4. //////////////////////////////////////////////////////////////
  5.  
  6. #include <windows.h>
  7. #include <d3dx9.h>
  8. #include <vector>
  9. #include "debug.h"
  10. #include "mouse.h"
  11. #include "sound.h"
  12.  
  13. class APPLICATION
  14. {
  15.     public:
  16.         APPLICATION();
  17.         HRESULT Init(HINSTANCE hInstance, int width, int height, bool windowed);
  18.         HRESULT Update(float deltaTime);
  19.         HRESULT Render();
  20.         HRESULT Cleanup();
  21.         HRESULT Quit();
  22.  
  23.     private:
  24.         IDirect3DDevice9* m_pDevice; 
  25.         MOUSE m_mouse;
  26.         SOUND m_sound;
  27.         LPD3DXSPRITE m_pSprite;
  28.         IDirect3DTexture9* m_pSoundTexture;
  29.  
  30.         HWND m_mainWindow;
  31.         ID3DXFont *m_pFont;
  32. };
  33.  
  34. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE prevInstance, PSTR cmdLine, int showCmd)
  35. {
  36.     APPLICATION app;
  37.  
  38.     if(FAILED(app.Init(hInstance, 800, 600, true)))
  39.         return 0;
  40.  
  41.     MSG msg;
  42.     memset(&msg, 0, sizeof(MSG));
  43.     int startTime = timeGetTime(); 
  44.  
  45.     while(msg.message != WM_QUIT)
  46.     {
  47.         if(::PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
  48.         {
  49.             ::TranslateMessage(&msg);
  50.             ::DispatchMessage(&msg);
  51.         }
  52.         else
  53.         {    
  54.             int t = timeGetTime();
  55.             float deltaTime = (t - startTime)*0.001f;
  56.  
  57.             app.Update(deltaTime);
  58.             app.Render();
  59.  
  60.             startTime = t;
  61.         }
  62.     }
  63.  
  64.     app.Cleanup();
  65.  
  66.     return msg.wParam;
  67. }
  68.  
  69. APPLICATION::APPLICATION()
  70. {
  71.     m_pDevice = NULL; 
  72.     m_mainWindow = 0;
  73.     m_pSoundTexture = NULL;
  74.     m_pSprite = NULL;
  75.  
  76.     srand(GetTickCount());
  77. }
  78.  
  79. HRESULT APPLICATION::Init(HINSTANCE hInstance, int width, int height, bool windowed)
  80. {
  81.     debug.Print("Application initiated");
  82.  
  83.     //Create Window Class
  84.     WNDCLASS wc;
  85.     memset(&wc, 0, sizeof(WNDCLASS));
  86.     wc.style         = CS_HREDRAW | CS_VREDRAW;
  87.     wc.lpfnWndProc   = (WNDPROC)::DefWindowProc; 
  88.     wc.hInstance     = hInstance;
  89.     wc.lpszClassName = "D3DWND";
  90.  
  91.     //Register Class and Create new Window
  92.     RegisterClass(&wc);
  93.     m_mainWindow = CreateWindow("D3DWND", "Example 13.1: Playing Sounds", WS_EX_TOPMOST, 0, 0, width, height, 0, 0, hInstance, 0); 
  94.     SetCursor(NULL);
  95.     ShowWindow(m_mainWindow, SW_SHOW);
  96.     UpdateWindow(m_mainWindow);
  97.  
  98.     //Create IDirect3D9 Interface
  99.     IDirect3D9* d3d9 = Direct3DCreate9(D3D_SDK_VERSION);
  100.  
  101.     if(d3d9 == NULL)
  102.     {
  103.         debug.Print("Direct3DCreate9() - FAILED");
  104.         return E_FAIL;
  105.     }
  106.  
  107.     //Check that the Device supports what we need from it
  108.     D3DCAPS9 caps;
  109.     d3d9->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &caps);
  110.  
  111.     //Hardware Vertex Processing or not?
  112.     int vp = 0;
  113.     if(caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT)
  114.         vp = D3DCREATE_HARDWARE_VERTEXPROCESSING;
  115.     else vp = D3DCREATE_SOFTWARE_VERTEXPROCESSING;
  116.  
  117.     //Check vertex & pixelshader versions
  118.     if(caps.VertexShaderVersion < D3DVS_VERSION(2, 0) || caps.PixelShaderVersion < D3DPS_VERSION(2, 0))
  119.     {
  120.         debug.Print("Warning - Your graphic card does not support vertex and pixelshaders version 2.0");
  121.     }
  122.  
  123.     //Set D3DPRESENT_PARAMETERS
  124.     D3DPRESENT_PARAMETERS d3dpp;
  125.     d3dpp.BackBufferWidth            = width;
  126.     d3dpp.BackBufferHeight           = height;
  127.     d3dpp.BackBufferFormat           = D3DFMT_A8R8G8B8;
  128.     d3dpp.BackBufferCount            = 1;
  129.     d3dpp.MultiSampleType            = D3DMULTISAMPLE_NONE;
  130.     d3dpp.MultiSampleQuality         = 0;
  131.     d3dpp.SwapEffect                 = D3DSWAPEFFECT_DISCARD; 
  132.     d3dpp.hDeviceWindow              = m_mainWindow;
  133.     d3dpp.Windowed                   = windowed;
  134.     d3dpp.EnableAutoDepthStencil     = true; 
  135.     d3dpp.AutoDepthStencilFormat     = D3DFMT_D24S8;
  136.     d3dpp.Flags                      = 0;
  137.     d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
  138.     d3dpp.PresentationInterval       = D3DPRESENT_INTERVAL_IMMEDIATE;
  139.  
  140.     //Create the IDirect3DDevice9
  141.     if(FAILED(d3d9->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, m_mainWindow,
  142.                                  vp, &d3dpp, &m_pDevice)))
  143.     {
  144.         debug.Print("Failed to create IDirect3DDevice9");
  145.         return E_FAIL;
  146.     }
  147.  
  148.     //Release IDirect3D9 interface
  149.     d3d9->Release();
  150.  
  151.     D3DXCreateFont(m_pDevice, 18, 0, 0, 1, false,  
  152.                    DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY,
  153.                    DEFAULT_PITCH | FF_DONTCARE, "Arial", &m_pFont);
  154.  
  155.     //Set sampler state
  156.     for(int i=0;i<4;i++)
  157.     {
  158.         m_pDevice->SetSamplerState(i, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
  159.         m_pDevice->SetSamplerState(i, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
  160.         m_pDevice->SetSamplerState(i, D3DSAMP_MIPFILTER, D3DTEXF_POINT);
  161.     }
  162.  
  163.     //Init mouse
  164.     m_mouse.InitMouse(m_pDevice, m_mainWindow);
  165.  
  166.     //Init Sound
  167.     m_sound.Init(m_mainWindow);
  168.  
  169.     //Load texture
  170.     D3DXCreateSprite(m_pDevice, &m_pSprite);
  171.     D3DXCreateTextureFromFile(m_pDevice, "textures/sounds.jpg", &m_pSoundTexture);
  172.  
  173.     return S_OK;
  174. }
  175.  
  176. HRESULT APPLICATION::Update(float deltaTime)
  177. {
  178.     //Update mouse
  179.     m_mouse.Update();
  180.  
  181.     //Change mouse
  182.     if(m_mouse.WheelUp())m_sound.SetMasterVolume(m_sound.GetMasterVolume() + 0.05f);
  183.     if(m_mouse.WheelDown())m_sound.SetMasterVolume(m_sound.GetMasterVolume() - 0.05f);
  184.  
  185.  
  186.     if(KEYDOWN(VK_ESCAPE))
  187.         Quit();
  188.  
  189.     return S_OK;
  190. }    
  191.  
  192. HRESULT APPLICATION::Render()
  193. {
  194.     // Clear the viewport
  195.     m_pDevice->Clear(0L, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0L );
  196.  
  197.  
  198.     // Begin the scene 
  199.     if(SUCCEEDED(m_pDevice->BeginScene()))
  200.     {
  201.         char number[50];
  202.         std::string sizeText = "Sound Volume: ";
  203.         sizeText += _itoa(m_sound.GetMasterVolume() * 10.0f, number, 10);
  204.         sizeText += "     (Mouse Wheel)";
  205.         RECT rc = {10, 10, 0, 0};
  206.         m_pFont->DrawText(NULL, sizeText.c_str(), -1, &rc, DT_LEFT| DT_TOP | DT_NOCLIP, 0xff000000);
  207.  
  208.         m_pSprite->Begin(0);
  209.         
  210.         RECT dest[] = {{136, 200, 264, 328}, {336, 200, 464, 328}, {536, 200, 664, 328}};
  211.  
  212.         for(int i=0;i<3;i++)
  213.         {
  214.             RECT src = {i * 128, 0, i * 128 + 128, 128};
  215.             if(m_mouse.Over(dest[i])){src.top = 128; src.bottom = 256;}
  216.         
  217.             m_pSprite->Draw(m_pSoundTexture, &src, NULL, &D3DXVECTOR3(dest[i].left, dest[i].top, 0.0f), 0xffffffff);
  218.  
  219.             if(m_mouse.PressInRect(dest[i]))
  220.             {
  221.                 m_sound.PlaySound(i, false);
  222.                 m_mouse.DisableInput(300);
  223.             }
  224.         }
  225.  
  226.         m_pSprite->End();
  227.  
  228.         //Draw mouse
  229.         m_mouse.Paint();
  230.  
  231.         // End the scene.
  232.         m_pDevice->EndScene();
  233.         m_pDevice->Present(0, 0, 0, 0);
  234.     }
  235.  
  236.     return S_OK;
  237. }
  238.  
  239. HRESULT APPLICATION::Cleanup()
  240. {
  241.     try
  242.     {
  243.         if(m_pSprite)m_pSprite->Release();
  244.         if(m_pSoundTexture)m_pSoundTexture->Release();
  245.  
  246.         m_pFont->Release();
  247.         m_pDevice->Release();
  248.  
  249.         debug.Print("Application terminated");
  250.     }
  251.     catch(...){}
  252.  
  253.     return S_OK;
  254. }
  255.  
  256. HRESULT APPLICATION::Quit()
  257. {
  258.     ::DestroyWindow(m_mainWindow);
  259.     ::PostQuitMessage(0);
  260.     return S_OK;
  261. }